using System.Collections.Generic;
using UnityEngine;

public class CachePools
{
    private Dictionary<GameObject, CachePool<IPoolObject>> cachePools =
        new Dictionary<GameObject, CachePool<IPoolObject>>();
    
    /// <summary>
    /// 获取对象
    /// </summary>
    /// <param name="prefab"></param>
    /// <param name="parent"></param>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetOne<T>(GameObject prefab,Transform parent)where T:IPoolObject
    {
        CachePool<IPoolObject> cachePool = GetCachePool(prefab, parent);
        T newItem = (T)cachePool.GetOrCreateOne<T>(false);
        cachePool.SetParent(newItem,parent);
        newItem.TurnOn();
        return newItem;
    }
    
    /// <summary>
    /// 回收对象
    /// </summary>
    /// <param name="instance"></param>
    /// <typeparam name="T"></typeparam>
    public void RecycleOne<T>(T instance) where T : IPoolObject
    {
        bool recycled = false;
        foreach (var cachePool in cachePools)
        {
            if (cachePool.Value.Contains(instance))
            {
                cachePool.Value.Recycle(instance);
                recycled = true;
                break;
            }
        }
        if (!recycled)
        {
            Debug.LogError(string.Format("没有对应容器，对象{0}无法被回收", instance));
            instance.DestroySelf();
        }
    }

    /// <summary>
    /// 移除实例
    /// </summary>
    /// <param name="instance"></param>
    /// <typeparam name="T"></typeparam>
    public void Remove<T>(T instance) where T : IPoolObject
    {
        foreach (var cachePool in cachePools)
        {
            if (cachePool.Value.Contains(instance))
            {
                cachePool.Value.Remove(instance);
                break;
            }
        }
    }
    
    /// <summary>
    /// 销毁所有
    /// </summary>
    public void DestroyAll()
    {
        if (cachePools != null)
        {
            foreach (var cachePool in cachePools)
            {
                cachePool.Value.DestroyAll();
            }
            cachePools.Clear();
        }
    }
    
    /// <summary>
    /// 释放未使用对象
    /// </summary>
    public void UnloadUnused()
    {
        if (cachePools != null)
        {
            foreach (var cachePool in cachePools)
            {
                cachePool.Value.Release();
            }
            cachePools.Clear();
        }
    }
    
    /// <summary>
    /// 销毁池子
    /// </summary>
    /// <param name="prefab"></param>
    public void DestroyPool(GameObject prefab)
    {
        if (cachePools.ContainsKey(prefab))
        {
            cachePools[prefab].DestroyAll();
            cachePools.Remove(prefab);
        }
    }

    CachePool<IPoolObject> GetCachePool(GameObject prefab,Transform parent)
    {
        if (cachePools.ContainsKey(prefab))
        {
            return cachePools[prefab];
        }
        CachePool<IPoolObject> cachePool = new CachePool<IPoolObject>(prefab, parent);
        cachePools.Add(prefab,cachePool);
        return cachePool;
    }
}